package com.qdlc.p2p.dal.dao.impl;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.dao.UserCreditDao;
import com.qdlc.p2p.dal.dto.UserCredit;

@Repository("userCreditDao")
public class UserCreditDaoImpl extends BaseDaoImpl<UserCredit> implements UserCreditDao {

	@Override
	public void update(double totalVar, double useVar, double nouseVar, long userId) {
		StringBuffer buffer = new StringBuffer(
				"UPDATE " + Global.DB_PREFIX + "user_credit SET credit=credit+:credit,credit_use=credit_use+:creditUse,credit_nouse=credit_nouse+:creditNouse WHERE user_id=:userId");
		Query query = em.createNativeQuery(buffer.toString());
		query.setParameter("credit", totalVar);
		query.setParameter("creditUse", useVar);
		query.setParameter("creditNouse", nouseVar);
		query.setParameter("userId", userId);
		int result = query.executeUpdate();
		if(result!=1){
			throw new BusinessException("更新用户信用额度失败！", 1);
		}
	}

	public UserCredit findByUserId(long userId) {
		return super.findObjByProperty("user.userId", userId);
	}
	
	

}
